<template>
  <div style="width: 100%;height: 100%">
    <LoginHuiFenRun v-if="id === 6" :title="title"></LoginHuiFenRun>
    <Login48 v-else-if="id === 48" :title="title"></Login48>
    <Login v-else :title="title"></Login>
  </div>
</template>

<script>

import { getDomainName } from '@/api/login'
import Login from '@/layout/components/login/login.vue'
import Login48 from '@/layout/components/login/login48.vue'
import LoginHuiFenRun from '@/layout/components/login/loginHuiFenRun.vue'

export default {
  components: {
    Login, LoginHuiFenRun, Login48
  },
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      id: 0,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getLogo()
  },
  methods: {
    getLogo() {
      const cachedData = JSON.parse(localStorage.getItem('domainData') || '{}');

      if (cachedData.id) {
        // 如果缓存数据存在，直接使用
        this.applyDomainData(cachedData);
      } else {
        // 如果缓存数据不存在，发起请求获取数据
        getDomainName({ domainName: window.location.hostname }).then(res => {
          if (res.code === 200 && res.data) {
            // 将数据存入 localStorage
            localStorage.setItem('domainData', JSON.stringify(res.data));
            this.applyDomainData(res.data);
          }
        });
      }
    },
    applyDomainData(data) {
      this.id = data.id
      this.title = data.loginName;
      document.title = data.platformTitle;
      let $favicon = document.querySelector('link[rel="icon"]');
      if ($favicon !== null) {
        $favicon.href = `${process.env.VUE_APP_BASE_API}${data.logo}`;
      }
    }
  }
};
</script>
